import axios from 'axios'
import Vue from 'vue'

const proto = Vue.prototype
const proURL = "http://114.116.250.121:8082"

// 创建实例时设置配置的默认值
const instance = axios.create({
  baseURL: proURL
})

// 拦截器
instance.interceptors.response.use(response => {
  const resData = response.data
  if (!resData.code) {
    if (resData.total_count) {
      resData.data = {
        data: resData.data,
        total_count: resData.total_count
      }
    }
    return resData.data
  } else {
    // 其他报错相关操作
    proto.$message({
      type: "error",
      message: resData.msg
    });

    return false
  }
}, (error) => {
  return Promise.reject(error)
})

export default {
  get(url, params) {
    return instance.get(url, { params })
  },
  post(url, params) {
    return instance.post(url, params)
  },
  put(url, params) {
    return instance.put(url, params)
  },
  file(url, params) {
    const config = {
      headers: { 'Content-Type': 'multipart/form-data' }
    }
    return instance.post(url, params, config)
  }
}
